package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.cache.CacheKey;
import cn.e4j.core.cache.CacheUtil;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.TreeUtil;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysModule;

import cn.e4j.modular.system.mapper.SysModuleMapper;

import cn.e4j.modular.system.service.SysModuleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模块管理
 *
 * @author yuhuan
 * @Date 2018/12/25
 */
@Controller
@RequestMapping("/sys/module")
public class SysModuleController extends BaseController {

    @Autowired
    private SysModuleMapper sysModuleMapper;


    @Autowired
    private SysModuleService sysModuleService;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:module:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/module.html";
    }

    /**
     * 跳转到查看新增的页面
     */
    @RequestMapping("/module_add")
    public String addView() {
        return "/pc/system/module_edit.html";
    }


    /**
     * 查询模块列表--树
     */
    @RequestMapping("/getModulesTree")
    @RequiresPermissions("sys:module:init")
    @Cacheable(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_MODULE_GETORGSTREE + "'")
    @ResponseBody
    public ResponseData getModulesTree() {
        List<SysModule> sysModules = sysModuleMapper.getModules(null);
        return ResponseData.success(TreeUtil.modulesTree(sysModules));
    }

    /**
     * 查询模块列表--树
     */
    @RequestMapping("/getModulesTreeByRole")
//    @RequiresPermissions("sys:module:init")
    //@Cacheable(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_MODULE_GETORGSTREE + "'")
    @ResponseBody
    public ResponseData getModulesTreeByRole() {
        ShiroUser shiroUser = ShiroKit.getUser();
        List<SysModule> sysModules = new ArrayList<>();
        if("9999999999999999".equals(shiroUser.getID())){ //超级管理员排除在外
            sysModules = sysModuleMapper.getModules(null);
        }else {
            sysModules = sysModuleService.getModulesByRole(shiroUser.getID());
        }
        return ResponseData.success(TreeUtil.modulesTree(sysModules));
    }

    /**
     * 查询模块列表
     */
    @RequestMapping("/getModuleslist")
    @RequiresPermissions("sys:module:init")
    @ResponseBody
    public ResponseData getModuleslist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysModuleService.getModuleslist(page, inDto);
        return new PageReData(page, result);
    }


    /**
     * 添加模块信息
     */
    @RequestMapping("/add")
    //@BussinessLog(value = "添加用户", key = "account", dict = UserDict.class)
    @RequiresPermissions("sys:module:init")
    @CacheEvict(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_MODULE_GETORGSTREE + "'", beforeInvocation = false)
    @ResponseBody
    public ResponseData add(SysModule sysModule, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Dto node = sysModuleService.add(sysModule);
        //返回
        //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_MODULE_GETORGSTREE);
        return new SuccessResponseData(node);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:module:init")
    //@BussinessLog(value = "修改用户", key = "account", dict = UserDict.class)
    @CacheEvict(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_MODULE_GETORGSTREE + "'", beforeInvocation = false)
    @ResponseBody
    public ResponseData del(SysModule sysModule, BindingResult result) {
        List childList = sysModuleService.getChildList(sysModule);
        if(childList.size() > 0){
            return new ErrorResponseData(BizExceptionEnum.MODULE_DELETE_ERROR.getCode(),
                    BizExceptionEnum.MODULE_DELETE_ERROR.getMessage());
        }else {
            sysModuleService.delete(sysModule);
            //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_MODULE_GETORGSTREE);
            return SUCCESS_TIP;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:module:init")
    //@BussinessLog(value = "修改用户", key = "account", dict = UserDict.class)
    @CacheEvict(value = CacheUtil.CONSTANT, key = "'" + CacheKey.C_SYS_MODULE_GETORGSTREE + "'", beforeInvocation = false)
    @ResponseBody
    public ResponseData edit(SysModule sysModule, BindingResult result) {
        sysModuleService.edit(sysModule);
        //CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.C_SYS_MODULE_GETORGSTREE);
        return SUCCESS_TIP;
    }


}
